<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Diplomacy' );
?>
					<div class="blok">
<?php
printDiplomacyTab();
?> 
					</div>
					<br />
<?php
$query = "SELECT turn, turns_inactive FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$turns_to_inactive = $row[ 'turns_inactive' ];
$current_turn = $row[ 'turn' ];
$query = "SELECT faction.faction_id, access, leader FROM faction_member JOIN faction ON faction.faction_id = faction_member.faction_id WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'faction_id' ] ) && $row[ 'access' ] > 0 ) {
	$leader = $row[ 'leader' ];
	print "<div id=\"blok\">\r\n";
	if( isset( $_SESSION[ 'member_invite_status' ] ) ) {
		print translate( "Status" ) . ": " . $_SESSION[ 'member_invite_status' ] . "<br /><br />\r\n";
		unset( $_SESSION[ 'member_invite_status' ] );
	}
	print( translate( "Invite a ruler" ) . ": <form method=\"post\" action=\"action/invite.php\"><input type=\"hidden\" name=\"faction_id\" value=\"" . $row[ 'faction_id' ] . "\" /><input type=\"text\" name=\"ruler_name\" /> <input type=\"submit\" value=\"" . translate( "Invite" ) . "\" /></form>" );
	print( "</div><br />\r\n" );
	
	$query2 = "SELECT x, y FROM village WHERE ruler_id = $leader AND start_place = 1";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$capital_x = $row2[ 'x' ];
	$capital_y = $row2[ 'y' ];
	
	$query3 = "SELECT ruler.ruler_id, ruler.name, score, active_turn FROM ruler " .
			"JOIN village ON village.ruler_id = ruler.ruler_id " .
			"WHERE start_place = 1 AND village.ruler_id != $leader AND x > $capital_x - 20 AND x < $capital_x + 20 AND y > $capital_y - 20 AND y < $capital_y + 20 " .
			"ORDER BY score DESC";
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result3 );
	$i = 0;
	$list = Array();
	while ( $i < $num ) {
		$query2 = "SELECT count(*) FROM faction_member " .
				"WHERE ruler_id = " . mysql_result( $result3, $i, "ruler_id" ) . " AND ( ( invited = 0 AND requested = 0 ) " .
				"OR ( faction_id = " . $row[ 'faction_id' ] . " AND ( invited = 1 OR requested = 1 ) ) )";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( !isset( $row2[ 'count(*)' ] ) || $row2[ 'count(*)' ] == 0 ) {
			$ruler = Array( 'ruler_id' => mysql_result( $result3, $i, "ruler_id" ), 
							'name' => mysql_result( $result3, $i, "name" ), 
							'score' => mysql_result( $result3, $i, "score" ),
							'active_turn' => mysql_result( $result3, $i, "active_turn" ) );
			$list[] = $ruler;
		}
		$i ++;
	}
	
	if( count( $list ) > 0 ) {
		print( "<table><th colspan=\"5\">" . translate( "Available rulers whom are close by" ) . "</th>\r\n" .
				"<tr><td>" . translate( "Rank" ) . "</td><td>" . translate( "Score" ) . "</td><td>" . translate( "Name" ) . "</td><td>" . translate( "Invite" ) . "</td></tr>\r\n" );
		foreach( $list as $ruler ) {
		    if( $i % 2 == 0 ) {
		        print "<tr id=\"even\">";
		    }
		    else {
		        print "<tr>";
		    }
			if( $ruler[ "active_turn" ] + $turns_to_inactive < $current_turn ) {
				$inactive = " (inactive)";
			}
			else {
				$inactive = "";
			}			
			$query2 = "SELECT count(*) FROM ruler WHERE score > " . $ruler[ 'score' ];
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

			print( "<td align=\"right\">" . number_format( $row2[ 'count(*)' ] + 1 ) . "</td><td align=\"right\">" . number_format( $ruler[ 'score' ] ) . "</td><td><a href=\"../overview/ruler.php?name=" . $ruler[ 'name' ] . "\">" . $ruler[ 'name' ] . "$inactive</a></td>" );
			print( "<td><form method=\"post\" action=\"action/invite.php\"><input type=\"hidden\" name=\"faction_id\" value=\"" . $row[ 'faction_id' ] . "\" /><input type=\"hidden\" name=\"ruler_name\" value=\"" . $ruler[ 'name' ] . "\" /><input type=\"submit\" value=\"" . translate( "Invite" ) . "\" /></form></td>" );
			print( "</tr>\r\n" );
		}
	
		print( "</table><br />\r\n" );
	}
	else {
		print( "<div class=\"blok\">\r\n" );
		print( translate( "There are no rulers close by, whom you can invite." ) . "\r\n" );
		print( "</div><br />\r\n" );
	}
	
	$query = "SELECT ruler_id FROM faction_member WHERE faction_id = " . $row[ 'faction_id' ] . " AND requested = 1";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
	
	if( $num > 0 ) {
		print( "<table><th colspan=\"5\">" . translate( "Rulers who requested to join" ) . ":</th>\r\n" .
				"<tr><td>" . translate( "Rank" ) . "</td><td>" . translate( "Score" ) . "</td><td>" . translate( "Name" ) . "</td><td>" . translate( "Accept" ) . "</td></tr>\r\n" );
		while ( $i < $num ) {
		    if( $i % 2 == 0 ) {
		        print "<tr id=\"even\">";
		    }
		    else {
		        print "<tr>";
		    }
			$query2 = "SELECT name, score, active_turn FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$ruler_name = $row2[ 'name' ];
			$score = $row2[ 'score' ];
			if( $row2[ "active_turn" ] + $turns_to_inactive < $current_turn ) {
				$inactive = " (inactive)";
			}
			else {
				$inactive = "";
			}			
			$query2 = "SELECT count(*) FROM ruler WHERE score > $score";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

			print( "<td align=\"right\">" . number_format( $row2[ 'count(*)' ] + 1 ) . "</td><td align=\"right\">" . number_format( $score ) . "</td><td><a href=\"../overview/ruler.php?name=" . $ruler_name . "\">$ruler_name$inactive</a></td>" );
			print( "<td><form method=\"post\" action=\"action/invite.php\"><input type=\"hidden\" name=\"faction_id\" value=\"" . $row[ 'faction_id' ] . "\" /><input type=\"hidden\" name=\"ruler_id\" value=\"" . mysql_result( $result, $i, "ruler_id" ) . "\" /><input type=\"submit\" value=\"" . translate( "Accept" ) . "\" /></form></td>" );
			print( "</tr>\r\n" );
			$i ++;
		}
		print( "</table><br />\r\n" );
	}
	else {
		print( "<div class=\"blok\">\r\n" );
		print( translate( "No rulers have requested to join your faction." ) . "\r\n" );
		print( "</div><br />\r\n" );
	}
	
	$query = "SELECT ruler_id FROM faction_member WHERE faction_id = " . $row[ 'faction_id' ] . " AND invited = 1";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
	if( $num > 0 ) {
		print( "<table><th colspan=\"5\">" . translate( "Invited rulers" ) . "</th>\r\n" .
				"<tr><td>" . translate( "Rank" ) . "</td><td>" . translate( "Score" ) . "</td><td>" . translate( "Name" ) . "</td><td>" . translate( "Uninvite" ) . "</td></tr>\r\n" );
		while ( $i < $num ) {
		    if( $i % 2 == 0 ) {
		        print "<tr id=\"even\">";
		    }
		    else {
		        print "<tr>";
		    }
			$query2 = "SELECT name, score, active_turn FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$ruler_name = $row2[ 'name' ];
			$score = $row2[ 'score' ];
			if( $row2[ "active_turn" ] + $turns_to_inactive < $current_turn ) {
				$inactive = " (inactive)";
			}
			else {
				$inactive = "";
			}
			$query2 = "SELECT count(*) FROM ruler WHERE score > $score";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			
			print( "<td align=\"right\">" . number_format( $row2[ 'count(*)' ] + 1 ) . "</td><td align=\"right\">" . number_format( $score ) . "</td><td><a href=\"../overview/ruler.php?name=" . $ruler_name . "\">$ruler_name$inactive</a></td>" );
			print( "<td><form method=\"post\" action=\"action/invite.php\"><input type=\"hidden\" name=\"uninvite\" value=\"uninvite\" /><input type=\"hidden\" name=\"faction_id\" value=\"" . $row[ 'faction_id' ] . "\" /><input type=\"hidden\" name=\"ruler_id\" value=\"" . mysql_result( $result, $i, "ruler_id" ) . "\" /><input type=\"submit\" value=\"" . translate( "Uninvite" ) . "\" /></form></td>" );
			print( "</tr>\r\n" );
			$i ++;
		}
		print( "</table>\r\n" );
	}	
	else {
		print( "<div class=\"blok\">\r\n" );
		print( translate( "No rulers have been invited to join your faction." ) . "\r\n" );
		print( "</div>\r\n" );
	}
}
else {
	print "<div class=\"blok\">\r\n";
	print( translate( "You are not allowed to view this." ) );
	print "</div>\r\n";
}
aos_footer();
?>